<?php

	session_start();
	include("sqlconnection.php");
	$resident = $_REQUEST["rusername"];
	$username = $_SESSION["username"];
	$db = new Database();
	$db->connect();
	
	$query = "select * from usertable where username = '$resident'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$residentName = $row['firstname'];
	$residentGender = $row['gender'];
	
	$query = "select * from usertable where username = '$username'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$userFirstname = $row['firstname'];
	
	$query = "select bid from users_behaviour where username = '$resident'";
	$result = mysql_query($query);
	$residentBehaviour = array();
	while($rows = mysql_fetch_array($result)){
		$bid = $rows["bid"];
		$residentBehaviour[] = $bid;
	}
	
	$query = "select bid from users_behaviour where username = '$username'";
	$result = mysql_query($query);
	$userBehaviour = array();
	while($rows = mysql_fetch_array($result)){
		$bid = $rows["bid"];
		$userBehaviour[] = $bid;
	}
	
	//difference and similarity in behaviours
	$diff = array_merge(array_diff($residentBehaviour,$userBehaviour), array_diff($userBehaviour,$residentBehaviour));
	//print "diff-dunno what";
	$sim = array_intersect($userBehaviour, $residentBehaviour);
	//user's beh diff from resident
	$udiff = array_diff($userBehaviour, $sim);
	//resident's beh diff from user
	$rdiff = array_diff($residentBehaviour, $sim);
	
	$query = "select bid from behaviour";
	$result = mysql_query($query);
	$behaviours = array();
	while($rows = mysql_fetch_array($result)){
		$behaviours[] = $rows["bid"];
	}
	
	$mergedDiffAndSim = array_merge($sim, $diff);
	
	//both users did not choose these behaviours
	$notChosenBehaviour = array_diff($behaviours, $mergedDiffAndSim);
	
	$mergedSimAndNotChoosen = array_merge($sim, $notChosenBehaviour);
	
	$percentage = (count($mergedSimAndNotChoosen)/10) * 100;
	
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Match</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link href="css/jq-mobile-green-button-theme.css" rel="stylesheet" type="text/css"/>
<link href="css/jq-mobile-red-button-theme.css" rel="stylesheet" type="text/css"/>
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
	<script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script>
</head>
<div data-role="page" id="page"  data-theme="b">
    <div data-role="header"><a data-rel="back" data-icon="back">Back</a>
     <a href="logout.php" data-role="button" data-icon="delete" rel="external">Logout</a>
        <h1 style='white-space:normal'>Compatibility <?php print $percentage . "%" ?></h1>
    </div>
    <div data-role="content">
    <?php 
		
	?>
        <ul data-role="listview" data-theme="c">
            <?php
				$query = "select * from behaviour";
				$result = mysql_query($query);
				$index =1;
				while($rows = mysql_fetch_array($result))
				{	
					$desc = $rows['desc'];
					print "<li>";
					print "<table style='width:100%'><tr>";
					print "<td style='width:30px'></td>";
					print "<td style='text-align:center; padding-left:5px; padding-right:5px;' valign='top'> $desc </td> ";
					print "<td  style='width:30px'></td>";
					print "</tr><tr>";
					foreach($sim as &$value){
						if($value == $rows['bid']){
							
							print "<td style='text-align:left'><img src='images/tick.png' width='20px' height='20px'/>You</td>";
							print "<td></td>";
							if($residentGender == "Male"){
								print "<td style='text-align:right'><img src='images/tick.png' width='20px' height='20px'/>Him</td>";
							}
							else{
								print "<td style='text-align:right'><img src='images/tick.png' width='20px' height='20px'/>Her</td>";
							}
						}
					}
	
					
				
					foreach($udiff as &$value)
					{
						if($value == $rows["bid"]){
							print "<td style='text-align:left'><img src='images/cross.png' width='20px' height='20px'/>You</td>";
							print "<td></td>";
							if($residentGender == "Male"){
								print "<td style='text-align:right'><img src='images/tick.png' width='20px' height='20px'/>Him</td>";
							}
							else{
								print "<td style='text-align:right'><img src='images/tick.png' width='20px' height='20px'/>Her</td>";
							}
						}
					}				
					
					foreach($rdiff as &$value)
					{
						if($value == $rows["bid"]){
							print "<td style='text-align:left'> <img src='images/tick.png' width='20px' height='20px'/>You</td>";
							print "<td></td>";
							if($residentGender == "Male"){
								print "<td style='text-align:right'><img src='images/cross.png' width='20px' height='20px'/>Him</td>";
							}
							else{
								print "<td style='text-align:right'><img src='images/cross.png' width='20px' height='20px'/>Her</td>";
							}
					
						}
					}

					foreach($notChosenBehaviour as &$value)
					{
						if($value == $rows["bid"]){
							print "<td style='text-align:left'> <img src='images/cross.png' width='20px' height='20px'/>You</td>";
							print "<td></td>";
							if($residentGender == "Male"){
								print "<td style='text-align:right'><img src='images/cross.png' width='20px' height='20px'/>Him</td>";
							}
							else{
								print "<td style='text-align:right'><img src='images/cross.png' width='20px' height='20px'/>Her</td>";
							}
						}
						
					}
					print "</tr></table>";
					print "</li>";			
				}
				?>
        </ul>
    </div>
</div>

<body>
</body>
</html>